# -*- Mode: Python -*- vi:si:et:sw=4:sts=4:ts=4:syntax=python

class Recipe(recipe.Recipe):
    name = 'spice-gtk'
    version = '0.39'
    btype = BuildType.MESON
    remotes = {'origin': 'https://gitlab.freedesktop.org/spice/spice-gtk.git'}
    commit = 'v%s' % version
    # either LGPLv2.1 or MPLv1.1
    licenses = [License.LGPLv2_1Plus]
    meson_options = {}

    deps = ['json-glib', 'glib', 'libpng', 'zlib', 'pixman', 'fontconfig', 'freetype', 'gtk-doc-lite',
            'gstreamer-1.0', 'gst-plugins-base-1.0', 'gst-plugins-good-1.0',
            'spice-protocol', 'libjpeg8', 'opus', 'six', 'openssl', 'liblz4', 'spice-usbredir']
    platform_deps = {
            Platform.WINDOWS: [ 'gtk+3', 'usbredir', 'phodav' ],
            Platform.LINUX: [ 'gtk+3', 'usbredir', 'phodav' ],
            Platform.DARWIN: [ 'gtk+3', 'phodav' ]
            }

    files_devel = ['include/spice-client-glib-2.0',
                   'lib/pkgconfig/spice-client-glib-2.0.pc']

    files_libs = ['libspice-client-glib-2.0']

    def prepare(self):
        self.meson_options['tests'] = 'false'
        self.meson_options['spice-common:tests'] = 'false'
        self.meson_options['smartcard'] = 'disabled'
        self.meson_options['vapi'] = 'disabled'
        self.meson_options['pulse'] = 'disabled'
        self.meson_options['gtk_doc'] = 'disabled'
        self.meson_options['builtin-mjpeg'] = 'true'
        self.meson_options['lz4'] = 'enabled'
        self.meson_options['opus'] = 'enabled'

        if self.config.target_platform in [Platform.IOS, 
                                           Platform.ANDROID]:
            self.meson_options['usbredir'] = 'enabled'
            self.meson_options['gtk_doc'] = 'disabled'
            self.meson_options['coroutine'] = 'gthread'
            self.patches = ['spice-gtk/0001-Disable-sync-of-audio-playback-and-record-volume.patch',
                            'spice-gtk/0002-Disable-gstaudio-record-channel.patch',
                            'spice-gtk/0003-Disable-notify-volume-and-notify-mute-for-playback-a.patch',
                            'spice-gtk/0004-Include-endian.h-to-fix-build-with-USB-redirection-o.patch',
                            'spice-gtk/0005-Implemented-the-ability-to-attach-USB-devices-by-ope.patch']
        if self.config.target_platform == Platform.DARWIN:
            self.meson_options['coroutine'] = 'gthread'
            self.meson_options['introspection'] = 'disabled'
            self.append_env('CPPFLAGS', '-xobjective-c')
        elif self.config.target_platform == Platform.WINDOWS:
            self.meson_options['usbredir'] = 'enabled'
            if self.config.target_arch == Architecture.X86:
                host = 'i686-w64-mingw32'
            else:
                host = 'x86_64-w64-mingw32'
            directx_headers = os.path.join(self.config.toolchain_prefix, host, 'include', 'directx')
            self.append_env('CPPFLAGS', ' -I' + directx_headers + ' -DWIN32_LEAN_AND_MEAN ${CPPFLAGS}')
            self.append_env('LIBS', ' -limm32 ${LIBS}')
        elif self.config.target_platform == Platform.LINUX:
            self.use_system_libs = True
            self.meson_options['usbredir'] = 'enabled'
            self.meson_options['usb-ids-path'] = './usb.ids'

    def post_install(self):
        if self.config.target_platform in [Platform.WINDOWS, Platform.LINUX]:
            shell.download("http://www.linux-usb.org/usb.ids",
                           destination=os.path.join(self.config.prefix, 'bin', 'usb.ids'))
